<template>
  <div class="jq-message-wrapper v-jq-message-fade" v-show="visible">
    <div class="jq-message-box">
      <div class="jq-message-header">
        <div class="jq-message-title">
          <span>{{ title }}</span>
        </div>
        <button class="jq-cancel-btns">
          <i class="el-message-box__close el-icon-close"></i>
        </button>
      </div>
      <div class="jq-message-content">
        <div class="jq-message-container">
          <div class="jq-message-message">
            <slot>
              <p>{{ message }}</p>
            </slot>
            <div class="jq-message-input">
              <el-input
                :placeholder="inputPlaceholder"
                @change="validator"
                @input="validator"
                v-if="showInput"
                v-model="inputValue"
                ref="input"
                :class="[validate ? 'invalid' : '', validate]"
              ></el-input>
            </div>
            <span style="color:red;size:10px" v-if="showInput && validate">{{
              inputErrorMessage
            }}</span>
          </div>
        </div>
      </div>
      <div class="jq-message-btns">
        <el-button
          :loading="confirmButtonLoading"
          type="primary"
          size="small"
          @click="handleAction('confirm')"
          @keyup.enter.native="handleAction('confirm')"
          ref="confirm"
          >{{ confirmButtonText || '确定' }}</el-button
        >
        <el-button
          type="primary"
          size="small"
          @click="handleAction('cancel')"
          v-if="showCancelButton"
          >{{ cancelButtonText || '取消' }}</el-button
        >
      </div>
    </div>
  </div>
</template>
<script>
import PopManager from '../utils/popup/popManager';
export default {
  watch: {
    visible(val) {
      if (val) {
        this.open();
        this.$nextTick(() => {
          this.$refs.confirm.$el.focus();
        });
      } else {
        this.close();
      }
    }
  },
  methods: {
    handleAction(action) {
      if (this.$type === 'prompt' && this.validate && action === 'confirm') return;
      this.action = action;
      if (typeof this.beforeClose === 'function') {
        this.beforeClose(action, this, this.doClose);
        return;
      }
      this.doClose();
    },
    validator(value) {
      this.validate = !this.inputPattern.test(value);
    },
    doClose() {
      if (this.$type === 'prompt' && this.validate) {
        this.validate = false;
        this.showInput = '';
      }
      this.callback(this.action, this);
      this.visible = false;
    },
    open() {
      let instance = this.$el;
      if (this.$type === 'prompt') {
        setTimeout(() => {
          this.$refs.input.focus();
        }, 0);
      }
      PopManager.openModal(instance, PopManager.nextzIndex());
      this.$el.style.zIndex = PopManager.nextzIndex();
    },

    close() {
      PopManager.closeModal();
    }
  },
  data() {
    return {
      uid: 1,
      title: undefined,
      message: '',
      type: '',
      iconClass: '',
      customClass: '',
      showInput: false,
      inputValue: null,
      inputPlaceholder: '',
      inputType: 'text',
      inputPattern: null,
      inputValidator: null,
      inputErrorMessage: '',
      showConfirmButton: true,
      showCancelButton: false,
      action: '',
      confirmButtonText: '',
      cancelButtonText: '',
      confirmButtonLoading: false,
      cancelButtonLoading: false,
      confirmButtonClass: '',
      confirmButtonDisabled: false,
      cancelButtonClass: '',
      editorErrorMessage: null,
      callback: null,
      dangerouslyUseHTMLString: false,
      focusAfterClosed: null,
      isOnComposition: false,
      distinguishCancelAndClose: false,
      visible: false,
      validate: false
    };
  }
};
</script>
<style lang="scss" scoped>
.jq-message-wrapper {
  position: fixed;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  text-align: center;
  &::after {
    content: ' ';
    height: 100%;
    width: 0;
    vertical-align: middle;
    display: inline-block;
  }
  .jq-message-box {
    display: inline-block;
    width: 420px;
    padding-bottom: 10px;
    vertical-align: middle;
    background-color: #fff;
    border-radius: 4px;
    border: 1px solid #ebeef5;
    font-size: 18px;
    box-shadow: 0 2px 12px 0 rgb(0 0 0 / 10%);
    text-align: left;
    overflow: hidden;
    backface-visibility: hidden;

    .jq-message-header {
      position: relative;
      padding: 15px 15px 10px;
      .jq-message-title {
        padding-left: 0;
        margin-bottom: 0;
        font-size: 18px;
        line-height: 1;
        color: #303133;
      }
      .jq-cancel-btns {
        position: absolute;
        top: 15px;
        right: 15px;
        padding: 0;
        border: none;
        outline: none;
        background: transparent;
        font-size: 16px;
        cursor: pointer;
      }
    }
    .jq-message-content {
      padding: 10px 15px;
      color: #606266;
      font-size: 14px;
      .jq-message-container {
        position: relative;
        .jq-message-message {
          margin: 0;
          .jq-message-input {
            padding-top: 15px;
            .invalid {
              ::v-deep .el-input__inner {
                border-color: #f56c6c;
              }
            }
          }
        }
      }
    }
    .jq-message-btns {
      padding: 5px 15px 0;
      text-align: right;
    }
  }
}
.jq-message-fade-enter-active {
  transition: all 0.5s;
}
.jq-message-fade-leave-active {
  transition: all 0.5s;
}
.jq-message-fade-enter,
.jq-message-fade-leave-to {
  transform: translateY(-20px);
  opacity: 0;
}
</style>
